package com.sf.wisdomkg.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sf.wisdomkg.R;
import com.sf.wisdomkg.adapter.TalkingParentListViewAdapter;
import com.sf.wisdomkg.utils.CommonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.Conversation;

/**
 * Created by ming on 2017/10/9.
 */
public class TalkingParentListActivity extends AppCompatActivity {
    private String baseUrl = CommonUtils.IP_S+ CommonUtils.M_T;

    //UI
    private Button back_btn;
    private TextView bar_content;
    private ListView talking_list;

    private ArrayList<HashMap<String, String>> list_view;
    private TalkingParentListViewAdapter parentList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_talking_parent_list);
        getSupportActionBar().hide();
        initView();
        getParentList();
        bar_content.setText("家长列表");
        back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        talking_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String tarId = list_view.get(i).get("phone");
                String name = list_view.get(i).get("children");

                RongIM.getInstance().startConversation(TalkingParentListActivity.this, Conversation.ConversationType.PRIVATE, tarId, name+"的家长");

            }
        });
    }
    /**
     * 初始化
     */
    private void initView(){
        back_btn = (Button)findViewById(R.id.back_btn);
        bar_content = (TextView) findViewById(R.id.bar_content);
        talking_list = (ListView)findViewById(R.id.talking_list);
        list_view = new ArrayList<>();
    }
    /**
     * 获取信息公告
     */
    private void getParentList() {

        RequestParams params = new RequestParams(baseUrl+ "parent/getIdAndPhone");
        //联网设置
        params.setCacheMaxAge(1000 * 60);
        params.setConnectTimeout(9000);
        System.out.println("得到的值"+params);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("得到的值"+result);
                //解析result
                if (result != null) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.optString("status").equals("true")) {
                            try {
                                JSONArray jsonArr = new JSONArray(jsonObject.optString("data"));

                                for (int i = 0; i < jsonArr.length(); i++) {
                                    HashMap<String, String> hashMap = new HashMap<>();
                                    JSONObject jsObj = new JSONObject();
                                    jsObj = jsonArr.getJSONObject(i);
                                    hashMap.put("id", jsObj.optString("id"));
                                    hashMap.put("name", jsObj.optString("name"));
                                    hashMap.put("age", jsObj.optString("age"));
                                    hashMap.put("job",jsObj.optString("job"));
                                    hashMap.put("phone",jsObj.optString("phone"));
                                    hashMap.put("address",jsObj.optString("address"));
                                    hashMap.put("sex",jsObj.optString("sex"));
                                    hashMap.put("img",jsObj.optString("img"));
                                    hashMap.put("children",jsObj.optString("children"));
                                    hashMap.put("childrenId",jsObj.optString("childrenId"));
                                    hashMap.put("password",jsObj.optString("password"));
                                    hashMap.put("isJK",jsObj.optString("isJK"));
                                    hashMap.put("page",jsObj.optString("page"));
                                    list_view.add(hashMap);
                                }
                                parentList = new TalkingParentListViewAdapter(TalkingParentListActivity.this, list_view);
                                talking_list.setAdapter(parentList);
                                parentList.notifyDataSetChanged();

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

                Toast.makeText(TalkingParentListActivity.this, "连接服务器失败,请检查网络！" + ex, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(TalkingParentListActivity.this, "请求超时,请检查网络！", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
